package com.iflytek.cosmo.ocr.common.json;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

/**
 * Jackson 工具类
 * 
 * @author ljhan2
 * @date 2018/03/26
 */
public abstract class Jackson {

    /**
     * 将一个对象序列化为JSON形式字符串
     * 
     * @param obj 待序列化对象
     * @return JSON形式字符串
     * @author ljhan2
     */
    public static String toJSONString(Object obj) {
        ObjectMapper mapper = ObjcetMapperBean.singletonNativeInstance();
        try {
            return mapper.writeValueAsString(obj);
        } catch (Throwable e) {
            throw new JacksonHandleJsonRuntimeException(e.getMessage(), e);
        }
    }

    public static <T> T parse2T(String json, TypeReference<T> typeRef) {
        ObjectMapper mapper = ObjcetMapperBean.singletonNativeInstance();
        try {
            return mapper.readValue(json, typeRef);
        } catch (Throwable e) {
            throw new JacksonHandleJsonRuntimeException(e.getMessage(), e);
        }
    }

    public static Object parse2Object(String json) {
        ObjectMapper mapper = ObjcetMapperBean.singletonNativeInstance();
        try {
            return mapper.readValue(json, new TypeReference<Object>() {});
        } catch (Throwable e) {
            throw new JacksonHandleJsonRuntimeException(e.getMessage(), e);
        }
    }

    /**
     * 将一个JSON字符串反序列化为一个Map
     * 
     * @param json
     * @return
     * @author ljhan2
     * @version
     * @since JDK 1.7
     */
    public static Map<String, Object> parse2Map(String json) {
        return parse2T(json, new TypeReference<Map<String, Object>>() {});
    }

    public static List<?> parse2List(String json) {
        return parse2T(json, new TypeReference<List<?>>() {});
    }

    /**
     * 将一个JSON字符串按指定类型反序列化为一个对象
     * 
     * @param <T> 类类型
     * @param json json字符串
     * @param clazz 对象Class对象
     * @return 对象
     * @author ljhan2
     */
    public static <T> T parse(String json, Class<T> clazz) {
        ObjectMapper mapper = ObjcetMapperBean.singletonNativeInstance();
        try {
            return mapper.readValue(json, clazz);
        } catch (Throwable e) {
            throw new JacksonHandleJsonRuntimeException(e.getMessage(), e);
        }
    }

    @SuppressWarnings("unchecked")
    public static <T> T parse(String json, Type type) {
        ObjectMapper mapper = ObjcetMapperBean.singletonNativeInstance();
        try {
            return (T)mapper.readValue(json, TypeFactory.rawClass(type));
        } catch (Throwable e) {
            throw new JacksonHandleJsonRuntimeException(e.getMessage(), e);
        }
    }

    public static JsonNode parseJsonNode(String json) {
        ObjectMapper mapper = ObjcetMapperBean.singletonNativeInstance();
        try {
            return mapper.readTree(json);
        } catch (Throwable e) {
            throw new JacksonHandleJsonRuntimeException(e.getMessage(), e);
        }
    }

    /**
     * 剥离重复转义JSON字符串，例如："[{\\\"key1\\\":\\\"value1\\\"}]"转换为"[{\"key1\":\"value1\"}]"
     * 
     * @param json 重复转义字符
     * @return 剥离后的JSON字符串
     * @author ljhan2
     */
    public static String stripRepeatEscape(String json) {
        if (null == json || json.length() < 1) {
            return json;
        }
        String result = json;
        if (json.contains("'")) {
            result = result.replace("{\\\'", "{\'");
            result = result.replace("\\\':", "\':");
            result = result.replace(":\\\'", ":\'");
            result = result.replace("\\\'}", "\'}");
        }
        if (json.contains("\"")) {
            result = result.replace("{\\\"", "{\"");
            result = result.replace("\\\":", "\":");
            result = result.replace(":\\\"", ":\"");
            result = result.replace("\\\"}", "\"}");
        }
        return result;
    }

}
